package com.situ.mall.controller;

import com.situ.mall.pojo.Category;
import com.situ.mall.pojo.query.CategoryQuery;
import com.situ.mall.pojo.vo.CategoryVO;
import com.situ.mall.service.ICategoryService;
import com.situ.mall.util.LayUITableResult;
import com.situ.mall.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private ICategoryService categoryService;

    @RequestMapping("/selectTopCategory")
    @ResponseBody
    public Result selectTopCategory() {
        List<Category> list = categoryService.selectTopCategory();
        return Result.ok(list);
    }

    @RequestMapping("/selectSecondCategory")
    @ResponseBody
    public Result selectSecondCategory(Integer parentId) {
        List<Category> list = categoryService.selectSecondCategory(parentId);
        return Result.ok(list);
    }

    @RequestMapping("/getCategoryListPage")
    public String getCategoryListPage() {
        return "category_list";
    }

    @RequestMapping("/selectByPage")
    @ResponseBody
    public LayUITableResult selectPage(CategoryQuery categoryQuery) {
        return categoryService.selectByPage(categoryQuery);
    }

    @RequestMapping("/getCategoryInsertPage")
    public String getCategoryInsertPage() {
        return "category_insert";
    }

    @RequestMapping("/insert")
    @ResponseBody
    public Result insert(Category category) {
        boolean isSuccess = categoryService.insert(category);
        return isSuccess ? Result.ok("添加成功") : Result.error("添加失败");
    }

    @RequestMapping("/deleteById")
    @ResponseBody
    public Result deleteById(Integer id) {
        boolean isSuccess = categoryService.deleteById(id);
        return isSuccess ? Result.ok("删除成功") : Result.error("删除失败");
    }

    @RequestMapping("/deleteAll")
    @ResponseBody
    public Result deleteAll(Integer[] ids) {
        boolean isSuccess = categoryService.deleteAll(ids);
        return isSuccess ? Result.ok("删除成功") : Result.error("删除失败");
    }

    @RequestMapping("/getUpdatePage")
    public String getUpdatePage(Integer id, Model model) {
        CategoryVO categoryVO = categoryService.selectById(id);
        System.out.println(categoryVO.toString());
        model.addAttribute("category", categoryVO);
        return "category_update";
    }

    @RequestMapping("/update")
    @ResponseBody
    public Result update(Category category) {
        boolean isSuccess = categoryService.update(category);
        return isSuccess ? Result.ok("修改成功") : Result.error("修改失败");
    }
}
